package customer.ejb.eaos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import authentication.ejb.usermanagement.User;
import common.Filter;
import common.IPredicate;
import model.GetEntities;
import model.Hotel;
import model.Invitation;
import model.PersonalizedTP;
import dtos.HotelDTO;
import dtos.InvitationDTO;

@Stateless
public class InvitationManagerEAO implements InvitationManager {
	
	@PersistenceContext
    private EntityManager em;
	
	private String inviter;
	
	public InvitationManagerEAO(){
		
	}


	@Override
	public int add(InvitationDTO i,int travelID, String invited, String inviter) {
		this.inviter = inviter;
		User user = em.find(User.class, inviter);
		PersonalizedTP travel = em.find(PersonalizedTP.class, travelID);		
		i.setInvited(invited);
		 i.setPersonalizedTp(travel.getDTO());
		 i.setUser(user.getUserDTO());
		 GetEntities ge = new GetEntities();
		 Invitation inv = ge.getInvitationEntity(i);
		em.persist(inv);
		em.flush();
		return inv.getInvitationID();
		
	}

	@Override
	public Collection<InvitationDTO> getGivenInvitationList(String inviter) {
		this.inviter = inviter;
		@SuppressWarnings("unchecked")
		List<Invitation> qres = em.createQuery("SELECT x FROM Invitation x").getResultList();
		Collection<Invitation> fres = Filter.filter(qres, isInviter);
		Collection<InvitationDTO> res = new ArrayList<InvitationDTO>(fres.size());
		
		
		for(Invitation i : fres)
		{
			res.add(i.getDTO());
		}
		
		return res;
		
	}
	
	IPredicate<Invitation> isInviter = new IPredicate<Invitation>() {
		public boolean apply(Invitation i) {
	        
	        return i.isInviter(inviter);
	    }

	};
	

	@Override
	public Collection<InvitationDTO> getReceivedInvitationList(String invited) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void removeInvitation(int invitationID) {
		Invitation i = em.find(Invitation.class,invitationID);
        em.remove(i);
		
	}
	

}
